<?php

    // Created By : Noan Babao
    // B.S. Computer Science - De La Salle Canlubang
    // October 27,2011
    //NOT IN parameters : documentType , documentName documentId , location
    //NOT OUT parameters : List of Attachments.

// Start - Checker for those users who will just go to the page by typing directly in the url.
if ($_POST) {
    // INCLUDES CONFIGURATION VARIABLES FOR DATABASE ACCESS
 $hostName = "localhost";
 $rootName = "dbasegr";
 $dBasePassword = "dbgr2012";
 $dBaseName = 'greenroute';

//    // CONNECT TO DATABASE
    $connect = mysql_connect($hostName,$rootName,$dBasePassword) or die ('Unable to connect!');
    mysql_select_db($dBaseName) or die ('Unable to select database!');

// GET VARIABLES VIA POST METHOD
    
$docName = $_POST['documentName'];
$docId = $_POST['documentId'];
$location =  $_POST['location'];
$documentTags =  explode(",",$_POST['documentTags']);
$attachmentName = explode(",",$_POST['attachmentName']);
$attachmentId = explode(",",$_POST['attachmentId']);
$routeId = $_POST['currentRoute'];
date_default_timezone_set('Asia/Singapore');
$currentDateTime= date("Y-m-d H:i:s" , time());
$mainDocumentLabel="";
$getMaxLabel = "
                        SELECT Count(*) as maxLabel
                        FROM Label

                   ";
    $result = mysql_query($getMaxLabel) or die ('Error in query: $getMaxLabel. ' . mysql_error());
    if (mysql_num_rows($result) > 0){
        while ($row = mysql_fetch_array($result)) {

            $maxLabel = $row['maxLabel'];

        }
    }


$queryCurrentStepId = "
                            SELECT *
                            FROM RouteStep
                            WHERE routeId = '$routeId' AND stepNumber = '2' AND subRouteId is NULL
                      ";

$result = mysql_query($queryCurrentStepId) or die ('Error in query: $queryCurrentStepId. ' . mysql_error());

if (mysql_num_rows($result) > 0){
    while ($row = mysql_fetch_array($result)) {

        $stepId = $row['stepId'];

    }

}

$queryMainDocument = 
            "
                INSERT into MainDocument 
                values('$docId', '$docName' , '$location', '$stepId' , 'Active','Sent');
            ";
 
 mysql_query($queryMainDocument) or die ('Error in query: $queryMainDocument. ' . mysql_error());
 
 
 
 $queryPreviousStepId = "
                            SELECT *
                            FROM RouteStep
                            WHERE routeId = '$routeId' AND stepNumber = '1' AND subRouteId is NULL
                      ";

$result = mysql_query($queryPreviousStepId) or die ('Error in query: $queryPreviousStepId. ' . mysql_error());

if (mysql_num_rows($result) > 0){
    while ($row = mysql_fetch_array($result)) {

        $currentStepId = $row['stepId'];

    }

}

 $queryLog = 
            "
                INSERT into Log (documentId, userId, stepId, logDateTime, activity) 
                values('$docId' , '$location' , $currentStepId , '$currentDateTime' , 'Tagged');
            ";
 
 mysql_query($queryLog) or die ('Error in query: $queryLog. ' . mysql_error());

 
 //add main document label!
 $queryExistingLabel = "
                SELECT *
                FROM Label
                WHERE label like '%$documentTags[0]%'
            ";
 
$result = mysql_query($queryExistingLabel) or die ('Error in query: $queryExistingLabel. ' . mysql_error());




if (mysql_num_rows($result) > 0){
    while ($row = mysql_fetch_array($result)) {

        $labelId = $row['labelId'];

    }
    $addDocumentLabel = "
                            INSERT INTO Document_Label
                            values('$docId' , '$labelId', '1');
                        ";
    mysql_query($addDocumentLabel) or die ('Error in query: $addDocumentLabel. ' . mysql_error());
    $mainDocumentLabel = $labelId;
}

else {
     $maxLabel++;
     $queryAddLabel ="
                        INSERT INTO Label
                        values('$maxLabel','$documentTags[0]')
                     ";

     mysql_query($queryAddLabel) or die ('Error in query: $queryAddLabel. ' . mysql_error());

     $queryAddDocumentLabel =   "
                                    INSERT INTO Document_Label
                                    values('$docId','$maxLabel','1');
                                ";
     mysql_query($queryAddDocumentLabel) or die ('Error in query: $queryAddDocumentLabel. ' . mysql_error());   
     $mainDocumentLabel = $maxLabel;
}


for($x=1; $x< count($documentTags) ;$x++)
{
    $queryExistingLabel = "
                    SELECT *
                    FROM Label
                    WHERE label like '%$documentTags[$x]%'
                ";

    $result = mysql_query($queryExistingLabel) or die ('Error in query: $queryExistingLabel. ' . mysql_error());




    if (mysql_num_rows($result) > 0){
        while ($row = mysql_fetch_array($result)) {

            $labelId = $row['labelId'];

        }
        $addDocumentLabel = "
                                INSERT INTO Document_Label
                                values('$docId' , '$labelId', '0');
                            ";
        mysql_query($addDocumentLabel) or die ('Error in query: $addDocumentLabel. ' . mysql_error());

    }

    else {
         $maxLabel++;
         $queryAddLabel ="
                            INSERT INTO Label
                            values('$maxLabel','$documentTags[$x]')
                         ";

         mysql_query($queryAddLabel) or die ('Error in query: $queryAddLabel. ' . mysql_error());

         $queryAddDocumentLabel =   "
                                        INSERT INTO Document_Label
                                        values('$docId','$maxLabel','0');
                                    ";
         mysql_query($queryAddDocumentLabel) or die ('Error in query: $queryAddDocumentLabel. ' . mysql_error());   
    }
}


$queryThisLabel = "
                        SELECT * 
                        FROM Document_Label 
                        WHERE documentId = '$docId'
                  ";

$result= mysql_query($queryThisLabel) or die ('Error in query: $queryThisLabel. ' . mysql_error());
$x=0;
if (mysql_num_rows($result) > 0){
        while ($row = mysql_fetch_array($result)) {
            
            $feed[$x]= $row['labelId'];
            $x++;
            
        }
 }

for($x=1;$x< count($attachmentId);$x++){
     $queryAttachment = 
            "
                INSERT into Attachment 
                values('$attachmentId[$x]', '$docId' , '$attachmentName[$x]', '$location' , 'Active');
            ";
    
    mysql_query($queryAttachment) or die ('Error in query: $queryAttachment. ' . mysql_error());
   
    
    $queryLog = 
            "
                INSERT into Log (documentId, userId, stepId, logDateTime, activity) 
                values('$attachmentId[$x]' , '$location' , '$currentStepId' , '$currentDateTime' , 'Tagged');
            ";
    
    mysql_query($queryLog) or die ('Error in query: $queryLog. ' . mysql_error());
    
    
 //   for($y=0;$y<count($feed);$y++){
         $addDocumentLabel = "
                                INSERT INTO Document_Label
                                values('$attachmentId[$x]' , '$mainDocumentLabel', '0');
                            ";
        mysql_query($addDocumentLabel) or die ('Error in query: $addDocumentLabel. ' . mysql_error());

 //   }
}

$queryLog = 
            "
                INSERT into Log (documentId, userId, stepId, logDateTime, activity)
                values( '$docId' , '$location' , $currentStepId , '$currentDateTime' , 'Sent');
            ";
 
 mysql_query($queryLog) or die ('Error in query: $queryLog. ' . mysql_error());

 for($x=1;$x< count($attachmentId);$x++){
     $queryLog = 
            "
                INSERT into Log (documentId, userId, stepId, logDateTime, activity)
                values( '$attachmentId[$x]' , '$location' , $currentStepId , '$currentDateTime' , 'Sent');
            ";
 
 mysql_query($queryLog) or die ('Error in query: $queryLog. ' . mysql_error());

 }




 mysql_close($connect);
}   // End - Checker for those users who will just go to the page by typing directly in the url.
else {
 echo "You are not authorized to view this page. This incident will be reported immediately.";
} 
?>
